package com.lsmgc.cms.util;

import com.alibaba.fastjson.JSON;
import com.lsmgc.cms.util.json.JsonReulst;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ServletUtils {
    public static final String PREFIX = "/WEB-INF/views/";
    public static final String SUFFIX = ".jsp";

    /**
     * 获取jsp页面
     * @param jspName
     * @return
     */
    public static String getJspPage(String jspName) {
        return PREFIX + jspName + SUFFIX;
    }

    /**
     * 发送内容
     * @param response
     * @param contentType
     * @param text
     */
    public static void render(HttpServletResponse response, String contentType, String text) {
        response.setContentType(contentType);
        try {
            response.getWriter().write(text);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送json
     * @param response
     * @param text
     */
    public static void renderJson(HttpServletResponse response, String text) {
        render(response, "application/json;charset=UTF-8", text);
    }

    /**
     * 发送xml
     * @param response
     * @param text
     */
    public static void renderXml(HttpServletResponse response, String text) {
        render(response, "text/xml;charset=UTF-8", text);
    }

    /**
     * 发送文本
     * @param response
     * @param text
     */
    public static void renderText(HttpServletResponse response, String text) {
        render(response, "text/plian;charset=UTF-8", text);
    }

    public static String getYesJson(Object entity) {
        return JSON.toJSONString(JsonReulst.yes(entity));
    }
    public static String getYesJson() {
        return JSON.toJSONString(JsonReulst.yes());
    }
    public static String getNoJson() {
        return JSON.toJSONString(JsonReulst.no());
    }
}
